clear
do "...\First.do"
********************************************************************************
use "$work\analysis_sample.dta"
keep if inrange(age,40,70)

g help=1 if inrange(timing,-4,0)
bys pnr: egen N_pre_2=total(help)
tab N_pre_2
keep if N_pre_2==5

drop if timing>3
drop if timing<-3


********************************************************************************
*** DDD
********************************************************************************
cap drop interaction2 interaction3 ddd3
g interaction2=low_ses*help3
g interaction3=low_ses*post
g ddd3=low_ses*help3*post

label var help3 "PCP low SES"
label var post "Post"
label var low_ses "Patient low SES"
label var interaction "PCP low SES x Post"
label var interaction2 "PCP low SES x Patient low SES"
label var interaction3 "Patient low SES x Post"
label var ddd3 "PCP low SES x Patient low SES x Post"


g mis_new=(yder_ses_missing>0 & timing==1)
bys pnr: egen max_mis=max(mis_new)

g SES=0 if help3==0  // Missing 
replace SES=1 if help3==0 & mis_new==0 // High non missing
replace SES=2 if help3==1 // Low non missing
tab SES


cap drop _SES
g _SES=SES if timing==1
cap drop SES
bys pnr: egen SES=max(_SES)
replace SES=0 if SES==.

replace statins=. if male==0
replace death_CVC=. if male==0

********************************************************************************


sum death if post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001)

ttest death if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death ib0.SES##i.low_ses $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)


outreg2 using "$table\TableA5.doc", replace tex label keep(1.SES#1.low_ses 2.SES#1.low_ses) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Death)



sum death_CVC if post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

ttest death_CVC if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

ttest death_CVC if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death_CVC ib0.SES##i.low_ses $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableA5.doc", append tex label keep(1.SES#1.low_ses 2.SES#1.low_ses) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Death from CVC)



sum N_visits if post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum N_visits if post==0 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest N_visits if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe N_visits post i.SES low_ses ib0.SES##post ib0.SES##interaction3 $patient_X, a(id year) cluster(id) 


outreg2 using "$table\TableA5.doc", append tex label keep(1.SES##1.interaction3 2.SES##1.interaction3) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0')  ctitle(Number of visits)


 


sum statins if post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum statins if post==0 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest statins if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe statins post i.SES low_ses ib0.SES##post ib0.SES##interaction3 $patient_X, a(id year) cluster(id)


outreg2 using "$table\TableA5.doc", append tex label keep(1.SES##1.interaction3 2.SES##1.interaction3) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0')  ctitle(Statins)




sum diabetes_control if post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum diabetes_control if post==0 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest diabetes_control if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe diabetes_control post i.SES low_ses ib0.SES##post ib0.SES##interaction3 $patient_X, a(id year) cluster(id) 


outreg2 using "$table\TableA5.doc", append tex label keep(1.SES##1.interaction3 2.SES##1.interaction3) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Diabetes Checkup)




sum ACSC_COPD if post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum ACSC_COPD if post==0 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest ACSC_COPD if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe ACSC_COPD post i.SES low_ses ib0.SES##post ib0.SES##interaction3 $patient_X, a(id year) cluster(id)  


outreg2 using "$table\TableA5.doc", append tex label keep(1.SES##1.interaction3 2.SES##1.interaction3) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Hospitalization COPD)

